﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KL.Store.Core.Domain;
using TN.Data;
using System.Web;
using KL.Store.Core;
using System.Globalization;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace KL.Store.Data
{
    public class dbXml
    {
        #region Get

        public static Xml Get(short XmlId)
        {
            IList<Xml> objectList = new List<Xml>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Xml_Get", objectList, new object[] { XmlId });
            return objectList.FirstOrDefault();
        }

        #endregion

        #region GetByName

        public static Xml GetByName(string Name)
        {
            IList<Xml> objectList = new List<Xml>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Xml_GetByName", objectList, new object[] { Name });
            return objectList.FirstOrDefault();
        }

        #endregion

        #region GetList

        public static IList<Xml> GetList()
        {
            IList<Xml> objectList = new List<Xml>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Xml_List", objectList);

            return objectList;
        }

        #endregion

        #region GetProductList

        public static IList<Product> GetProductList(short XmlId)
        {
            IList<Product> objectList = new List<Product>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Xml_List_Product", objectList, new object[] { XmlId });
            return objectList;
        }

        #endregion

        #region GetIPList

        public static IList<XmlIP> GetIPList(short XmlId)
        {
            IList<XmlIP> objectList = new List<XmlIP>();
            SqlHelper.FillList(dbParameters.dsnStore, "SPR_ESTORE_Xml_IP_List", objectList, new object[] { XmlId });

            return objectList;
        }

        #endregion

        #region TodaysAccessCount

        public static int TodaysAccessCount(string IPAddress, int XmlId)
        {
            int result = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_Xml_TodaysAccessCount", new object[] { IPAddress, XmlId });
            return result;
        }

        #endregion

        #region IPAccessLog

        public static int IPAccessLog(string IPAddress, int XmlId)
        {
            int result = (int)SqlHelper.ExecuteScalar(dbParameters.dsnStore, "SPR_ESTORE_Xml_IPAccessLog", new object[] { IPAddress, XmlId });
            return result;
        }

        #endregion

    }

    #region XmlHandler

    public class XmlHandler : IHttpHandler
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            string extension = context.Request.CurrentExecutionFilePathExtension.ToLower();

            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            if (context.Request.CurrentExecutionFilePathExtension.Equals(".xml"))
            {
                context.Response.ContentType = "application/xml";
            }
            else if (context.Request.CurrentExecutionFilePathExtension.Equals(".xls"))
            {
                context.Response.ContentType = "application/vnd.ms-excel";
                context.Response.AppendHeader("content-disposition", "attachment; filename=" + context.Request.FilePath.Replace("/", ""));
            }

            if (extension == ".xml" || extension == ".xls")
            {

                Xml xml = dbXml.GetByName(context.Request.FilePath.Replace("/", ""));

                if (xml != null)
                {

                    string clientIP = context.Request.UserHostAddress ?? "";

                    if (xml.IPRestriction)
                    {
                        #region Check IP Restriction

                        IList<XmlIP> IPs = dbXml.GetIPList(xml.XmlId);
                        if (IPs != null && IPs.Count > 0)
                        {


                            Logger.Write("XML Export Request for IP : " + clientIP + " [" + xml.Description + "]", "Trace");

                            if (IPs.Where(w => w.IP == clientIP).Count() == 0)
                            {
                                context.Response.Write("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                                context.Response.Write("<aciklama>");
                                context.Response.Write("Tanımsız IP : " + context.Request.UserHostAddress);
                                context.Response.Write("</aciklama>");
                                context.Response.Flush();

                                Logger.Write("IP not Defined for XML Export : " + clientIP + " [" + xml.Description + "]", "Trace");

                                return;
                            }
                        }

                        #endregion
                    }

                    if (xml.DailyAccessPermission > 0)
                    {
                        #region Daily Access Permission

                        var todaysaccess = dbXml.TodaysAccessCount(clientIP, xml.XmlId);
                        if (todaysaccess >= xml.DailyAccessPermission)
                        {
                            context.Response.Write("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                            context.Response.Write("<aciklama>");
                            context.Response.Write("Gunluk XML Erisim Hakki Asildi - " + context.Request.UserHostAddress);
                            context.Response.Write("</aciklama>");
                            context.Response.Flush();
                            Logger.Write("Daily Access Limit Exceed - XML Export Request for IP : " + clientIP + " [" + xml.Description + "]", "Trace");
                            return;
                        }

                        #endregion
                    }

                    var logid = dbXml.IPAccessLog(clientIP, xml.XmlId);

                    string siteurl = dbRefs.GetRef1(1913) == "E" ? Parameters.WebSiteUrl.Replace("http://", "https://") : Parameters.WebSiteUrl;

                    context.Response.Write(xml.Body);
                    context.Response.Write(xml.Body2);
                    context.Response.Flush();

                    #region Used Fields

                    bool extraFields = xml.Detail.IndexOf("{Extra}") > -1;
                    bool variantValues = xml.Detail.IndexOf("{Variant}") > -1;
                    bool FieldId = xml.Detail.IndexOf("{Id}") > -1;
                    bool FieldName = xml.Detail.IndexOf("{Name}") > -1;
                    bool FieldPrice = xml.Detail.IndexOf("{Price}") > -1;
                    bool FieldPriceWithCargo = xml.Detail.IndexOf("{PriceWithCargo}") > -1;

                    bool FieldPriceWithTransferAndCargo = xml.Detail.IndexOf("{PriceWithTransferAndCargo}") > -1;
                    bool FieldPriceWithTransferAndCargo3 = xml.Detail.IndexOf("{PriceWithTransferAndCargo3}") > -1;
                    bool FieldPriceWithTransferAndCargo5 = xml.Detail.IndexOf("{PriceWithTransferAndCargo5}") > -1;
                    bool FieldPriceWithTransferAndCargo8 = xml.Detail.IndexOf("{PriceWithTransferAndCargo8}") > -1;
                    bool FieldPriceWithTransferAndCargo10 = xml.Detail.IndexOf("{PriceWithTransferAndCargo10}") > -1;
                    bool FieldPriceWithTransferAndCargo12 = xml.Detail.IndexOf("{PriceWithTransferAndCargo12}") > -1;
                    bool FieldPriceWithTransferAndCargo15 = xml.Detail.IndexOf("{PriceWithTransferAndCargo15}") > -1;
                    bool FieldPriceWithTransferAndCargo18 = xml.Detail.IndexOf("{PriceWithTransferAndCargo18}") > -1;
                    bool FieldPriceWithTransferAndCargo20 = xml.Detail.IndexOf("{PriceWithTransferAndCargo20}") > -1;

                    bool FieldPriceWithTaxAndCargo = xml.Detail.IndexOf("{PriceWithTaxAndCargo}") > -1;
                    bool FieldPriceWithTaxAndCargo3 = xml.Detail.IndexOf("{PriceWithTaxAndCargo3}") > -1;
                    bool FieldPriceWithTaxAndCargo5 = xml.Detail.IndexOf("{PriceWithTaxAndCargo5}") > -1;
                    bool FieldPriceWithTaxAndCargo8 = xml.Detail.IndexOf("{PriceWithTaxAndCargo8}") > -1;
                    bool FieldPriceWithTaxAndCargo10 = xml.Detail.IndexOf("{PriceWithTaxAndCargo10}") > -1;
                    bool FieldPriceWithTaxAndCargo12 = xml.Detail.IndexOf("{PriceWithTaxAndCargo12}") > -1;
                    bool FieldPriceWithTaxAndCargo15 = xml.Detail.IndexOf("{PriceWithTaxAndCargo15}") > -1;
                    bool FieldPriceWithTaxAndCargo18 = xml.Detail.IndexOf("{PriceWithTaxAndCargo18}") > -1;
                    bool FieldPriceWithTaxAndCargo20 = xml.Detail.IndexOf("{PriceWithTaxAndCargo20}") > -1;

                    bool FieldCurrency = xml.Detail.IndexOf("{Currency}") > -1;

                    bool FieldPriceWithTax = xml.Detail.IndexOf("{PriceWithTax}") > -1;
                    bool FieldPriceWithTax3 = xml.Detail.IndexOf("{PriceWithTax3}") > -1;
                    bool FieldPriceWithTax5 = xml.Detail.IndexOf("{PriceWithTax5}") > -1;
                    bool FieldPriceWithTax8 = xml.Detail.IndexOf("{PriceWithTax8}") > -1;
                    bool FieldPriceWithTax10 = xml.Detail.IndexOf("{PriceWithTax10}") > -1;
                    bool FieldPriceWithTax12 = xml.Detail.IndexOf("{PriceWithTax12}") > -1;
                    bool FieldPriceWithTax15 = xml.Detail.IndexOf("{PriceWithTax15}") > -1;
                    bool FieldPriceWithTax18 = xml.Detail.IndexOf("{PriceWithTax18}") > -1;
                    bool FieldPriceWithTax20 = xml.Detail.IndexOf("{PriceWithTax20}") > -1;

                    bool FieldTax = xml.Detail.IndexOf("{Tax}") > -1;
                    bool FieldBrand = xml.Detail.IndexOf("{Brand}") > -1;
                    bool FieldCategory = xml.Detail.IndexOf("{Category}") > -1;
                    bool FieldCategoryId = xml.Detail.IndexOf("{CategoryId}") > -1;
                    bool FieldParentCategory = xml.Detail.IndexOf("{ParentCategory}") > -1;
                    bool FieldParentCategoryId = xml.Detail.IndexOf("{ParentCategoryId}") > -1;
                    bool FieldProductCode = xml.Detail.IndexOf("{ProductCode}") > -1;
                    bool FieldInStockAmount = xml.Detail.IndexOf("{InStockAmount}") > -1;
                    bool FieldAllStockAmount = xml.Detail.IndexOf("{AllStockAmount}") > -1;
                    bool FieldInStockAmountInt = xml.Detail.IndexOf("{InStockAmountInt}") > -1;
                    bool FieldAllStockAmountInt = xml.Detail.IndexOf("{AllStockAmountInt}") > -1;
                    bool FieldImageURL = xml.Detail.IndexOf("{ImageURL}") > -1;
                    bool FieldUrl = xml.Detail.IndexOf("{Url}") > -1;
                    bool FieldModifiedDate = xml.Detail.IndexOf("{ModifiedDate}") > -1;
                    bool FieldModel = xml.Detail.IndexOf("{Model}") > -1;
                    bool FieldWarantyPeriod = xml.Detail.IndexOf("{WarantyPeriod}") > -1;
                    bool FieldWarantyTime = xml.Detail.IndexOf("{WarantyTime}") > -1;
                    bool FieldDeliveryPeriod = xml.Detail.IndexOf("{DeliveryPeriod}") > -1;
                    bool FieldDeliveryTime = xml.Detail.IndexOf("{DeliveryTime}") > -1;
                    bool FieldCargoPrice = xml.Detail.IndexOf("{CargoPrice}") > -1;
                    bool FieldCargoPriceWithTax = xml.Detail.IndexOf("{CargoPriceWithTax}") > -1;
                    bool FieldCargoPriceField = xml.Detail.IndexOf("{CargoPriceField}") > -1;
                    bool FieldCargoPriceAdditionField = xml.Detail.IndexOf("{CargoPriceAdditionField}") > -1;
                    bool FieldCargo = xml.Detail.IndexOf("{Cargo}") > -1;
                    bool FieldFastCargo = xml.Detail.IndexOf("{FastCargo}") > -1;
                    bool FieldNewProduct = xml.Detail.IndexOf("{NewProduct}") > -1;
                    bool FieldFreeCargo = xml.Detail.IndexOf("{FreeCargo}") > -1;
                    bool FieldPriceWithTransfer = xml.Detail.IndexOf("{PriceWithTransfer}") > -1;
                    bool FieldBuyingPrice = xml.Detail.IndexOf("{BuyingPrice}") > -1;
                    bool FieldDesi = xml.Detail.IndexOf("{Desi}") > -1;
                    bool FieldWeight = xml.Detail.IndexOf("{Weight}") > -1;
                    bool FieldWidth = xml.Detail.IndexOf("{Width}") > -1;
                    bool FieldLength = xml.Detail.IndexOf("{Length}") > -1;
                    bool FieldHeight = xml.Detail.IndexOf("{Height}") > -1;
                    bool FieldDescription = xml.Detail.IndexOf("{Description}") > -1;
                    bool FieldDescription2 = xml.Detail.IndexOf("{Description2}") > -1;
                    bool FieldExternalDescription = xml.Detail.IndexOf("{ExternalDescription}") > -1;
                    bool FieldPrice1 = xml.Detail.IndexOf("{Price1}") > -1;
                    bool FieldPrice2 = xml.Detail.IndexOf("{Price2}") > -1;
                    bool FieldPrice3 = xml.Detail.IndexOf("{Price3}") > -1;
                    bool FieldPrice1WithTax = xml.Detail.IndexOf("{Price1WithTax}") > -1;
                    bool FieldPrice2WithTax = xml.Detail.IndexOf("{Price2WithTax}") > -1;
                    bool FieldPrice3WithTax = xml.Detail.IndexOf("{Price3WithTax}") > -1;
                    bool FieldPrice1WithTaxAndCargo = xml.Detail.IndexOf("{Price1WithTaxAndCargo}") > -1;
                    bool FieldPrice2WithTaxAndCargo = xml.Detail.IndexOf("{Price2WithTaxAndCargo}") > -1;
                    bool FieldPrice3WithTaxAndCargo = xml.Detail.IndexOf("{Price3WithTaxAndCargo}") > -1;
                    bool FieldBuyingCurrency = xml.Detail.IndexOf("{BuyingCurrency}") > -1;
                    bool FieldIsActive = xml.Detail.IndexOf("{IsActive}") > -1;
                    bool FieldBarcode = xml.Detail.IndexOf("{Barcode}") > -1;

                    #endregion

                    if (xml.IncludeDynamicPages)
                    {
                        IList<Page> PageList = dbPage.GetListContent();

                        foreach (var page in PageList)
                        {
                            context.Response.Write(xml.Detail.Replace("{Url}", siteurl + page.Url).Replace("{ModifiedDate}", string.IsNullOrEmpty(page.DuzelTar) ? page.GirisTar : page.DuzelTar));
                        }
                    }

                    IList<Product> ProductList = dbXml.GetProductList(xml.XmlId);
                    string imageurl = Parameters.WebSiteImageUrl;

                    foreach (var product in ProductList)
                    {
                        string tempText = xml.Detail;

                        #region Fields Replace

                        if (FieldId) { tempText = tempText.Replace("{Id}", product.ProductId.ToString()); }
                        if (FieldName) { tempText = tempText.Replace("{Name}", product.ProductName); }
                        if (FieldBuyingPrice) { tempText = tempText.Replace("{BuyingPrice}", product.DealerSellingPrice.ToString(new CultureInfo("en-US"))); }

                        if (FieldPrice) { tempText = tempText.Replace("{Price}", product.Price.ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTransfer) { tempText = tempText.Replace("{PriceWithTransfer}", product.TransferPrice > 0 ? product.TransferPrice.ToString(new CultureInfo("en-US")) : product.PriceWithTax.ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithCargo) { tempText = tempText.Replace("{PriceWithCargo}", (product.Price + (product.FreeCargo ? 0 : product.CargoPriceWithTax)).ToString(new CultureInfo("en-US"))); }

                        if (FieldPriceWithTax) { tempText = tempText.Replace("{PriceWithTax}", product.PriceWithTax.ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTax3) { tempText = tempText.Replace("{PriceWithTax3}", Math.Round(product.PriceWithTax * (decimal)1.03, 2).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTax5) { tempText = tempText.Replace("{PriceWithTax5}", Math.Round(product.PriceWithTax * (decimal)1.05, 2).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTax8) { tempText = tempText.Replace("{PriceWithTax8}", Math.Round(product.PriceWithTax * (decimal)1.08, 2).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTax10) { tempText = tempText.Replace("{PriceWithTax10}", Math.Round(product.PriceWithTax * (decimal)1.10, 2).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTax12) { tempText = tempText.Replace("{PriceWithTax12}", Math.Round(product.PriceWithTax * (decimal)1.12, 2).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTax15) { tempText = tempText.Replace("{PriceWithTax15}", Math.Round(product.PriceWithTax * (decimal)1.15, 2).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTax18) { tempText = tempText.Replace("{PriceWithTax18}", Math.Round(product.PriceWithTax * (decimal)1.18, 2).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTax20) { tempText = tempText.Replace("{PriceWithTax20}", Math.Round(product.PriceWithTax * (decimal)1.20, 2).ToString(new CultureInfo("en-US"))); }


                        if (FieldPriceWithTaxAndCargo) { tempText = tempText.Replace("{PriceWithTaxAndCargo}", (product.PriceWithTax + (product.FreeCargo ? 0 : product.CargoPriceWithTax)).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTaxAndCargo3) { tempText = tempText.Replace("{PriceWithTaxAndCargo3}", Math.Round((product.PriceWithTax + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.03, 2).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTaxAndCargo5) { tempText = tempText.Replace("{PriceWithTaxAndCargo5}", Math.Round((product.PriceWithTax + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.05, 2).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTaxAndCargo8) { tempText = tempText.Replace("{PriceWithTaxAndCargo8}", Math.Round((product.PriceWithTax + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.08, 2).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTaxAndCargo10) { tempText = tempText.Replace("{PriceWithTaxAndCargo10}", Math.Round((product.PriceWithTax + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.10, 2).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTaxAndCargo12) { tempText = tempText.Replace("{PriceWithTaxAndCargo12}", Math.Round((product.PriceWithTax + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.12, 2).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTaxAndCargo15) { tempText = tempText.Replace("{PriceWithTaxAndCargo15}", Math.Round((product.PriceWithTax + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.15, 2).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTaxAndCargo18) { tempText = tempText.Replace("{PriceWithTaxAndCargo18}", Math.Round((product.PriceWithTax + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.18, 2).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTaxAndCargo20) { tempText = tempText.Replace("{PriceWithTaxAndCargo20}", Math.Round((product.PriceWithTax + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.20, 2).ToString(new CultureInfo("en-US"))); }

                        if (FieldPriceWithTransferAndCargo) { tempText = tempText.Replace("{PriceWithTransferAndCargo}", product.TransferPrice > 0 ? (product.TransferPrice + (product.FreeCargo ? 0 : product.CargoPriceWithTax)).ToString(new CultureInfo("en-US")) : (product.PriceWithTax + (product.FreeCargo ? 0 : product.CargoPriceWithTax)).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTransferAndCargo3) { tempText = tempText.Replace("{PriceWithTransferAndCargo3}", product.TransferPrice > 0 ? ((product.TransferPrice + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.03).ToString(new CultureInfo("en-US")) : ((product.PriceWithTax + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.03).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTransferAndCargo5) { tempText = tempText.Replace("{PriceWithTransferAndCargo5}", product.TransferPrice > 0 ? ((product.TransferPrice + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.05).ToString(new CultureInfo("en-US")) : ((product.PriceWithTax + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.05).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTransferAndCargo8) { tempText = tempText.Replace("{PriceWithTransferAndCargo8}", product.TransferPrice > 0 ? ((product.TransferPrice + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.08).ToString(new CultureInfo("en-US")) : ((product.PriceWithTax + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.08).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTransferAndCargo10) { tempText = tempText.Replace("{PriceWithTransferAndCargo10}", product.TransferPrice > 0 ? ((product.TransferPrice + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.10).ToString(new CultureInfo("en-US")) : ((product.PriceWithTax + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.10).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTransferAndCargo12) { tempText = tempText.Replace("{PriceWithTransferAndCargo12}", product.TransferPrice > 0 ? ((product.TransferPrice + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.12).ToString(new CultureInfo("en-US")) : ((product.PriceWithTax + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.12).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTransferAndCargo15) { tempText = tempText.Replace("{PriceWithTransferAndCargo15}", product.TransferPrice > 0 ? ((product.TransferPrice + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.15).ToString(new CultureInfo("en-US")) : ((product.PriceWithTax + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.15).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTransferAndCargo18) { tempText = tempText.Replace("{PriceWithTransferAndCargo18}", product.TransferPrice > 0 ? ((product.TransferPrice + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.18).ToString(new CultureInfo("en-US")) : ((product.PriceWithTax + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.18).ToString(new CultureInfo("en-US"))); }
                        if (FieldPriceWithTransferAndCargo20) { tempText = tempText.Replace("{PriceWithTransferAndCargo20}", product.TransferPrice > 0 ? ((product.TransferPrice + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.20).ToString(new CultureInfo("en-US")) : ((product.PriceWithTax + (product.FreeCargo ? 0 : product.CargoPriceWithTax)) * (decimal)1.20).ToString(new CultureInfo("en-US"))); }

                        if (FieldCargoPrice) { tempText = tempText.Replace("{CargoPrice}", (product.FreeCargo ? 0 : product.CargoPrice).ToString(new CultureInfo("en-US"))); }
                        if (FieldCargoPriceWithTax) { tempText = tempText.Replace("{CargoPriceWithTax}", (product.FreeCargo ? 0 : product.CargoPriceWithTax).ToString(new CultureInfo("en-US"))); }
                        if (FieldCurrency) { tempText = tempText.Replace("{Currency}", product.Currency); }
                        if (FieldTax) { tempText = tempText.Replace("{Tax}", product.TaxRate.ToString(new CultureInfo("en-US"))); }
                        if (FieldBrand) { tempText = tempText.Replace("{Brand}", product.Brand); }
                        if (FieldCategory) { tempText = tempText.Replace("{Category}", product.Category); }
                        if (FieldCategoryId) { tempText = tempText.Replace("{CategoryId}", product.CategoryId.ToString()); }
                        if (FieldParentCategory) { tempText = tempText.Replace("{ParentCategory}", product.ParentCategory); }
                        if (FieldParentCategoryId) { tempText = tempText.Replace("{ParentCategoryId}", product.ParentCategoryId.ToString()); }
                        if (FieldProductCode) { tempText = tempText.Replace("{ProductCode}", product.ProductCode); }
                        if (FieldInStockAmount) { tempText = tempText.Replace("{InStockAmount}", (product.InStock < 0 ? 0 : product.InStock).ToString("0.#####")); }
                        if (FieldAllStockAmount) { tempText = tempText.Replace("{AllStockAmount}", ((product.InStock < 0 ? 0 : product.InStock) + product.StockOnTheWay).ToString("0.#####")); }

                        if (FieldInStockAmountInt) { tempText = tempText.Replace("{InStockAmountInt}", Math.Truncate(product.InStock < 0 ? 0 : product.InStock).ToString()); }
                        if (FieldAllStockAmountInt) { tempText = tempText.Replace("{AllStockAmountInt}", Math.Truncate((product.InStock < 0 ? 0 : product.InStock) + product.StockOnTheWay).ToString()); }

                        if (FieldImageURL) { tempText = tempText.Replace("{ImageURL}", imageurl + "/product-big/" + (product.ProductImageCount > 0 ? product.ProductId.ToString() : product.UstUrunId.ToString()) + "_1.jpg"); }
                        if (FieldUrl) { tempText = tempText.Replace("{Url}", siteurl + product.LinkUrl); }
                        if (FieldModifiedDate) { tempText = tempText.Replace("{ModifiedDate}", string.IsNullOrEmpty(product.ModifiedDate) ? product.CreatedDate : product.ModifiedDate); }
                        if (FieldModel) { tempText = tempText.Replace("{Model}", product.Model); }
                        if (FieldWarantyPeriod) { tempText = tempText.Replace("{WarantyPeriod}", product.VarrantyPeriod); }
                        if (FieldWarantyTime) { tempText = tempText.Replace("{WarantyTime}", product.VarrantyTime.ToString()); }
                        if (FieldDeliveryPeriod) { tempText = tempText.Replace("{DeliveryPeriod}", product.DeliveryPeriod); }
                        if (FieldDeliveryTime) { tempText = tempText.Replace("{DeliveryTime}", ((product.InStock > 0 && Parameters.XmlExportDefaultDeliveryTimeInStock > 0) ? Parameters.XmlExportDefaultDeliveryTimeInStock.ToString() : (product.DeliveryTime == null || product.DeliveryTime == 0 ? (Parameters.XmlExportDefaultDeliveryTime > 0 ? Parameters.XmlExportDefaultDeliveryTime.ToString() : "") : product.DeliveryTime.ToString()))); }
                        if (FieldCargoPriceField) { tempText = tempText.Replace("{CargoPriceField}", (product.FreeCargo ? 0 : product.CargoPriceField).ToString(new CultureInfo("en-US"))); }
                        if (FieldCargoPriceAdditionField) { tempText = tempText.Replace("{CargoPriceAdditionField}", (product.FreeCargo ? 0 : product.CargoPriceAddition).ToString(new CultureInfo("en-US"))); }
                        if (FieldCargo) { tempText = tempText.Replace("{Cargo}", product.FreeCargo ? "KARGO SATICIYA AIT" : "KARGO ALICIYA AIT"); }
                        if (FieldFastCargo) { tempText = tempText.Replace("{FastCargo}", product.FastCargo ? "1" : "0"); }
                        if (FieldNewProduct) { tempText = tempText.Replace("{NewProduct}", product.NewProduct ? "1" : "0"); }
                        if (FieldFreeCargo) { tempText = tempText.Replace("{FreeCargo}", product.FreeCargo ? "1" : "0"); }
                        if (FieldDesi) { tempText = tempText.Replace("{Desi}", product.Desi.ToString(new CultureInfo("en-US"))); }
                        if (FieldWeight) { tempText = tempText.Replace("{Weight}", product.Weight.ToString(new CultureInfo("en-US"))); }
                        if (FieldWidth) { tempText = tempText.Replace("{Width}", product.Width.ToString(new CultureInfo("en-US"))); }
                        if (FieldLength) { tempText = tempText.Replace("{Length}", product.Length.ToString(new CultureInfo("en-US"))); }
                        if (FieldHeight) { tempText = tempText.Replace("{Height}", product.Height.ToString(new CultureInfo("en-US"))); }
                        if (FieldDescription) { tempText = tempText.Replace("{Description}", product.Description.ToString().Replace("\r\n", "<br />")); }
                        if (FieldDescription2) { tempText = tempText.Replace("{Description2}", product.Description.Replace("<li>", "").Replace("</li>", "||").Replace("\r\n", "")).ToString(); }
                        if (FieldExternalDescription) { tempText = tempText.Replace("{ExternalDescription}", product.Description2.ToString()); }

                        if (FieldPrice1) { tempText = tempText.Replace("{Price1}", product.Price1.ToString(new CultureInfo("en-US"))); }
                        if (FieldPrice2) { tempText = tempText.Replace("{Price2}", product.Price2.ToString(new CultureInfo("en-US"))); }
                        if (FieldPrice3) { tempText = tempText.Replace("{Price3}", Math.Round(product.Price3, 0).ToString(new CultureInfo("en-US"))); }
                        if (FieldPrice1WithTax) { tempText = tempText.Replace("{Price1WithTax}", (product.Price1 * (1 + (product.TaxRate / 100))).ToString(new CultureInfo("en-US"))); }
                        if (FieldPrice2WithTax) { tempText = tempText.Replace("{Price2WithTax}", (product.Price2 * (1 + (product.TaxRate / 100))).ToString(new CultureInfo("en-US"))); }
                        if (FieldPrice3WithTax) { tempText = tempText.Replace("{Price3WithTax}", Math.Round((product.Price3 * (1 + (product.TaxRate / 100))), 0).ToString(new CultureInfo("en-US"))); }
                        if (FieldPrice1WithTaxAndCargo) { tempText = tempText.Replace("{Price1WithTax}", ((product.Price1 * (1 + (product.TaxRate / 100))) + (product.FreeCargo ? 0 : product.CargoPriceWithTax)).ToString(new CultureInfo("en-US"))); }
                        if (FieldPrice2WithTaxAndCargo) { tempText = tempText.Replace("{Price2WithTax}", ((product.Price2 * (1 + (product.TaxRate / 100))) + (product.FreeCargo ? 0 : product.CargoPriceWithTax)).ToString(new CultureInfo("en-US"))); }
                        if (FieldPrice3WithTaxAndCargo) { tempText = tempText.Replace("{Price3WithTax}", (Math.Round((product.Price3 * (1 + (product.TaxRate / 100))), 0) + (product.FreeCargo ? 0 : product.CargoPriceWithTax)).ToString(new CultureInfo("en-US"))); }
                        if (FieldBuyingCurrency) { tempText = tempText.Replace("{BuyingCurrency}", product.BuyingCurrency); }
                        if (FieldIsActive) { tempText = tempText.Replace("{IsActive}", product.IsActive ? "AKTIF" : "PASIF"); }
                        if (FieldBarcode) { tempText = tempText.Replace("{Barcode}", product.Barcode); }

                        #endregion

                        #region Extra Fields

                        if (extraFields)
                        {
                            StringBuilder sbExtra = new StringBuilder();
                            IList<ProductInfo> pInfos = dbProductInfo.GetList(product.ProductId);
                            if (pInfos == null || pInfos.Count == 0)
                            {
                                sbExtra.AppendLine("");
                                //sbExtra.AppendLine(xml.ExtraDetail.Replace("{ExtraKey}", "").Replace("{ExtraValue}", ""));
                            }
                            foreach (ProductInfo pInfo in pInfos)
                            {
                                string pInfoText = xml.ExtraDetail;
                                pInfoText = pInfoText.Replace("{ExtraKey}", pInfo.Type).Replace("{ExtraKeyShort}", StrShorter(pInfo.Type)).Replace("{ExtraValue}", pInfo.Information).Replace("{ExtraValueShort}", StrShorter(pInfo.Information));
                                sbExtra.AppendLine(pInfoText);
                            }
                            string extraBody = xml.ExtraBody.Replace("{ExtraDetail}", sbExtra.ToString());
                            tempText = tempText.Replace("{Extra}", extraBody);
                        }

                        #endregion

                        #region Variant Values

                        if (variantValues)
                        {
                            var variants = dbProduct.GetProductVariantListById(product.ProductId);
                            tempText = tempText.Replace("{Variant}", (variants != null ? variants.FirstOrDefault().value : ""));
                        }


                        #endregion

                        context.Response.Write(tempText.Replace("<![CDATA[]]>", "").Replace("&", "&amp;"));
                        context.Response.Flush();
                    }
                }
                else
                {
                    context.Response.Write("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                    context.Response.Write("<aciklama>");
                    context.Response.Write("Dosya tanımlı değil!");
                    context.Response.Write("</aciklama>");
                    context.Response.Flush();

                    Logger.Write("XML isteği yapılan dosya tanımlı değil : " + context.Request.FilePath.Replace("/", ""), "Trace");

                    return;
                }

                context.Response.Write(xml.BodyEnd);
                context.Response.Flush();

                //context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(xml.RenewInterval * 60));
                //context.Response.Cache.SetCacheability(HttpCacheability.Server);
                //context.Response.Cache.SetValidUntilExpires(true);
            }
        }

        public string StrShorter(string str)
        {
            str = str.ToLower().Replace("ş", "s").Replace("ü", "u").Replace("ö", "o").Replace("ı", "i").Replace("ç", "c").Replace("ğ", "g").Replace("/", "").Replace("+", "").Replace("%", "").Replace("*", "").Replace("#", "").Replace("-", "").Replace(" ", "").Replace("&", "").Replace("\"", "").Replace("'", "").Replace("/", "").Replace("_", "");
            return str;
        }

        #endregion
    }

    #endregion
}
